package jee.boot.common.config;

import com.google.common.collect.Lists;
import jee.boot.entity.auth.Account;
import jee.boot.common.properties.ProjectConfig;
import jee.boot.common.remote.RemoteEndpoint;
import jee.boot.common.standard.AccountHandler;
import jee.boot.common.standard.AuthorityHandler;
import jee.boot.common.utils.JeeBootContext;
import jee.boot.entity.auth.Role;
import jee.boot.entity.auth.User;
import org.apache.commons.beanutils.PropertyUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.CacheManager;

import java.util.List;

/**
 * 默认的用户授权信息获取方式,从缓存中获取
 */
public class DefaultAuthorityHandlerImpl implements AuthorityHandler {
    private Logger logger = LoggerFactory.getLogger(getClass());
    private CacheManager cacheManager;
    private ProjectConfig projectConfig;
    private AccountHandler accountHandler;
    private RemoteEndpoint remoteEndpoint;

    public DefaultAuthorityHandlerImpl(CacheManager cacheManager, ProjectConfig projectConfig, AccountHandler accountHandler, RemoteEndpoint remoteEndpoint) {
        this.cacheManager = cacheManager;
        this.projectConfig = projectConfig;
        this.accountHandler=accountHandler;
        this.remoteEndpoint=remoteEndpoint;

    }

    /**
     * 当前登录的用户信息,包含了所有的资源,数据权限
     *
     * @return
     */
    @Override
    public User getCurrentUser() {
        String accountId = JeeBootContext.get(JeeBootContext.AUTHORIZED_USER);
        if (accountId != null) {
            return getUserByAccount(accountId);
        }
        Account account = accountHandler.getPrincipal();
        if (account!=null) {
           return getUserByAccount(account.getId());
        }
        return null;
    }

    /**
     * 根据Account信息获取用户的权限,包含了所有的资源,数据权限
     *
     * @param accountId
     * @return
     */
    @Override
    public User getUserByAccount(String accountId) {
        if (cacheManager.getCache(AuthorityHandler.USER_AUTHORITY_KEY) == null || cacheManager.getCache(AuthorityHandler.USER_AUTHORITY_KEY).get(projectConfig.getName() + ":" + accountId) == null) {
            remoteEndpoint.executePost(projectConfig.getOpenOauthServer(), "/user/authorization", new Account(accountId), User.class);
        }
        return (User)cacheManager.getCache(AuthorityHandler.USER_AUTHORITY_KEY).get(projectConfig.getName() + ":" + accountId).get();
    }

    /**
     * 当前登录User的ID
     *
     * @return
     */
    @Override
    public String getUserId() {
        return getProperty("id");
    }

    /**
     * 当前登录User所拥有的所有角色
     *
     * @return
     */
    @Override
    public List<Role> getRoleList() {
        List<Role> roleList = getProperty("roleList");
        if(roleList==null){
            return Lists.newArrayList();
        }
        return roleList;
    }
    /**
     * 当前登录User中的其他属性
     * @param name 属性名称
     * @param <T> 返回数据类型
     * @return
     */
    @Override
    public <T> T getProperty(String name) {
        User user = getCurrentUser();
        if (user != null) {
            try {
                return (T) PropertyUtils.getProperty(user, name);
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
                return null;
            }
        }
        return null;
    }

}
